﻿using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ExamUtils;
using ExamCommon;
using ExamClientBLL;

namespace Exam_Client
{
    public partial class FrmLogin : Form
    {
        LoginService loginService = new LoginService();

        // 声明客户端主窗体
        FrmMainClient frmMainClient;

        // 定义一个委托集合, 用于业务逻辑层控制UI
        public static List<Action<string>> actions = new List<Action<string>>();

        public FrmLogin()
        {
            InitializeComponent();
            //Control.CheckForIllegalCrossThreadCalls = false;  // 如果关闭检查,则其他线程也能操作UI
            actions.Add(ShowTips);  // actions[0] 表示登录出错执行的提示操作
            actions.Add(LoginSuccess); // actions[2] 表示登录成功时执行的操作
            // 将相应的委托给业务逻辑层
            LoginService.initAction(actions);
        }

        #region 拖动控件窗体
        // 拖动控件窗体
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        #endregion

        /// <summary>
        /// 窗口拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 登录按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_login_BtnClick(object sender, EventArgs e)
        {
            // 合法性校验
            if (CheckContent() == false)
            {
                return;
            }
            // 获取界面登录信息
            LoginDTO loginDTO = GetContent();

            // 向服务器发起登录申请
            bool sendSeccess = LoginService.LoginRequset(loginDTO);
            if (sendSeccess == false)
            {
                FrmDialog.ShowDialog(this, "连接服务器失败! 请检查信息是否正确!");
                return;
            }
        }

        /// <summary>
        /// 内容校验
        /// </summary>
        /// <returns></returns>
        private bool CheckContent()
        {
            bool flag = true;
            if (!CheckUtils.CheckStudentNo(Converter.ToTrimedString(txt_studentNo.Text)))
            {
                FrmAnchorTips.ShowTips(txt_studentNo, "格式错误", AnchorTipsLocation.RIGHT, Color.FromArgb(254, 240, 240), Color.FromArgb(245, 108, 108), null, 10, 2000);
                flag = false;
            }
            if (string.IsNullOrEmpty(txt_name.Text))
            {
                FrmAnchorTips.ShowTips(txt_name, "不能为空", AnchorTipsLocation.RIGHT, Color.FromArgb(254, 240, 240), Color.FromArgb(245, 108, 108), null, 10, 2000);
                flag = false;
            }
            if (!CheckUtils.CheckIPAddress(Converter.ToTrimedString(txt_ipAddress.Text)))
            {
                FrmAnchorTips.ShowTips(txt_ipAddress, "格式错误", AnchorTipsLocation.RIGHT, Color.FromArgb(254, 240, 240), Color.FromArgb(245, 108, 108), null, 10, 2000);
                flag = false;
            }
            int port = -1;
            try
            {
                port = Convert.ToInt32(Converter.ToTrimedString(txt_port.Text));
                if (!CheckUtils.CheckPort(port))
                {
                    FrmAnchorTips.ShowTips(txt_port, "格式错误", AnchorTipsLocation.RIGHT, Color.FromArgb(254, 240, 240), Color.FromArgb(245, 108, 108), null, 10, 2000);
                    flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("校验端口号异常: " + ex.Message);
                FrmAnchorTips.ShowTips(txt_port, "格式错误", AnchorTipsLocation.RIGHT, Color.FromArgb(254, 240, 240), Color.FromArgb(245, 108, 108), null, 10, 2000);
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <returns></returns>
        private LoginDTO GetContent()
        {
            LoginDTO loginDTO = new LoginDTO();
            loginDTO.name = Converter.ToTrimedString(txt_name.Text);
            loginDTO.studentNo = Converter.ToTrimedString(txt_studentNo.Text);
            loginDTO.ipAddress = Converter.ToTrimedString(txt_ipAddress.Text);
            loginDTO.port = Convert.ToInt32(Converter.ToTrimedString(txt_port.Text));
            return loginDTO;
        }

        /// <summary>
        /// 给用户响应的提示
        /// </summary>
        /// <param name="str"></param>
        public void ShowTips(string str)
        {
            // 这里使用invoke调用ui响应
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    ShowInfo(str);
                }));
            }
        }

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="str"></param>
        private void ShowInfo(string str)
        {
            FrmDialog.ShowDialog(this, str);
        }

        /// <summary>
        /// 登录成功时执行的操作
        /// </summary>
        /// <param name="s"></param>
        private void LoginSuccess(string s)
        {
            // 这里使用invoke调用ui响应
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    frmMainClient = new FrmMainClient();
                    this.Hide();
                    frmMainClient.Show();
                    frmMainClient.ShowLoginSeccessTips();
                }));
            }
        }


        /// <summary>
        /// 窗体失去焦点就自动关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoseFocusClose(object sender, EventArgs e)
        {
            ((Form)sender).Close();
        }
        /// <summary>
        /// 回车登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_port_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btn_login_BtnClick(null, null);
            }
        }


        private void btn_close_MouseEnter(object sender, EventArgs e)
        {
            this.btn_close.BackColor = Color.FromArgb(245, 108, 108);
        }

        private void btn_close_MouseLeave(object sender, EventArgs e)
        {
            this.btn_close.BackColor = Color.White;
        }

        private void btn_login_MouseEnter(object sender, EventArgs e)
        {
            this.btn_login.FillColor = Color.FromArgb(70, 130, 180);
            this.btn_login.RectColor = Color.FromArgb(70, 130, 180);
            this.btn_login.FillColor = Color.FromArgb(70, 130, 180);
        }

        private void btn_login_MouseLeave(object sender, EventArgs e)
        {
            this.btn_login.FillColor = Color.DodgerBlue;
        }
    }
}
